/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\defgroup   le_unicast_manager
\brief      The events sent from the le_unicast_manager using the Event Bus
*/
#if defined(INCLUDE_LE_AUDIO_UNICAST)

#include "le_unicast_send_event.h"
#include "le_audio_core_channel_event_spec.h"
#include "le_audio_profile_channel_event_spec.h"
#include "event_bus_publish.h"
#include <gatt_connect.h>
#include <logging.h>

/*! \brief Send message through Event Bus.

 \param event event bus event to publish.
 \param data data associated with the event
 \param data_size size of the data

 */
static void leUnicastManager_PublishEvent(le_audio_core_event_t event, void * data, uint16 data_size)
{
    EventBus_Publish(EVENT_BUS_CHANNEL_LE_AUDIO_CORE, event, data, data_size);
}

void leUnicastManager_PublishConnectionStatus(uint32 cid, uint16 uuid, int status)
{
    le_audio_profile_event_connection_state_change_data_t evt_data;

    evt_data.uuid = uuid;
    evt_data.connection_handle = GattConnect_GetHandleFromConnectionId(cid);
    evt_data.status = status;
    EventBus_Publish(EVENT_BUS_CHANNEL_LE_AUDIO_BAP_PROFILE, le_audio_profile_event_connection_state_change, &evt_data, sizeof(evt_data));
}

void leUnicastManager_SendDisconnectionCompleteEvent(uint16 connection_handle, hci_status reason, acl_link_type_t link_type)
{
    LE_AUDIO_CORE_CIS_DISCONNECTION_COMPLETE_T event = {0};

    event.status = hci_success;
    event.connection_handle = connection_handle;
    event.reason = reason;
    event.type = link_type;
    DEBUG_LOG("leUnicastManager_SendDisconnectionCompleteEvent");
    leUnicastManager_PublishEvent(le_audio_core_cis_disconnection_complete, &event, sizeof(LE_AUDIO_CORE_DISCONNECTION_COMPLETE_T));
}

void leUnicastManager_SendCisEstablishedEvent(const BapServerCisEstablishedInd *ind)
{
    LE_AUDIO_CORE_CIS_ESTABLISHED_T event = {0};

    event.subevent_code = 0x19;
    event.status = hci_success;
    event.conHandle = ind->cisHandle;
    event.cigSyncDelay= ind->cisParams.cigSyncDelay;
    event.cisSyncDelay= ind->cisParams.cisSyncDelay;
    event.transportLatencyC2P= ind->cisParams.transportLatencyMtoS;
    event.transportLatencyP2C= ind->cisParams.transportLatencyStoM;
    event.phyC2P= ind->cisParams.phyMtoS;
    event.phyP2C= ind->cisParams.phyStoM;
    event.nse= ind->cisParams.nse;
    event.bnC2P= ind->cisParams.bnMtoS;
    event.bnP2C= ind->cisParams.bnStoM;
    event.ftC2P= ind->cisParams.ftMtoS;
    event.ftP2C= ind->cisParams.ftStoM;
    event.maxPduC2P= ind->cisParams.maxPduMtoS;
    event.maxPduP2C= ind->cisParams.maxPduStoM;
    event.isoInterval= ind->cisParams.isoInterval;

    DEBUG_LOG("leUnicastManager_SendCisEstablishedEvent");
    leUnicastManager_PublishEvent(le_audio_core_cis_established, &event, sizeof(LE_AUDIO_CORE_CIS_ESTABLISHED_T));
}

void leUnicastManager_SendSetupDataPathCommandCompletEvent(const BapServerSetupDataPathCfm *cfm)
{
    LE_AUDIO_CORE_COMMAND_COMPLETE_T event = {0};

    event.command = le_audio_core_setup_iso_data_path;
    event.status = cfm->status;
    event.conn_handle = cfm->isoHandle;

    DEBUG_LOG("leUnicastManager_SendSetupDataPathCommandCompletEvent");
    leUnicastManager_PublishEvent(le_audio_core_command_complete, &event, sizeof(LE_AUDIO_CORE_COMMAND_COMPLETE_T));
}

void leUnicastManager_SendRemoveDataPathCommandCompletEvent(const BapServerRemoveDataPathCfm *cfm)
{
    LE_AUDIO_CORE_COMMAND_COMPLETE_T event = {0};

    event.command = le_audio_core_remove_iso_data_path;
    event.status = cfm->status;
    event.conn_handle = cfm->isoHandle;

    DEBUG_LOG("leUnicastManager_SendRemoveDataPathCommandCompletEvent");
    leUnicastManager_PublishEvent(le_audio_core_command_complete, &event, sizeof(LE_AUDIO_CORE_COMMAND_COMPLETE_T));
}

void leUnicastManager_SendCisRequestEvent(uint16 acl_handle, uint16 cis_handle,
                                          uint8 cig_id, uint8 cis_id)
{
    LE_AUDIO_CORE_CIS_REQUEST_T event = {0};

    event.subevent_code = 0x1A;
    event.acl_conn_Handle = acl_handle;
    event.cis_conn_Handle = cis_handle;
    event.cig_id= cig_id;
    event.cis_id= cis_id;

    DEBUG_LOG("leUnicastManager_SendCisRequestEvent");
    leUnicastManager_PublishEvent(le_audio_core_cis_request, &event, sizeof(LE_AUDIO_CORE_CIS_REQUEST_T));
}

#endif /* defined(INCLUDE_LE_AUDIO_UNICAST) */

